//
//  HWSegmentView.m
//  嗨文
//
//  Created by 响当当 on 2019/1/25.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWSegmentView.h"

@interface HWSegmentView()
@property (nonatomic, strong)NSArray *titleArr;//标题
@property (nonatomic, strong)NSMutableArray *buttonArr;//标题存储
@end

@implementation HWSegmentView

- (NSArray *)titleArr
{
    if (!_titleArr)
    {
        _titleArr = @[@"阅读口味",@"男生小说",@"女生小说"];
    }
    return _titleArr;
}

- (NSMutableArray *)buttonArr
{
    if (!_buttonArr)
    {
        _buttonArr = [[NSMutableArray alloc]init];
    }
    return _buttonArr;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self creatSegment];
    }
    return self;
}

- (void)creatSegment
{
    CGFloat width = (SCREEN_WIDTH-120*kUIScaleBasePlus)/3;
    CGFloat height = 15*kUIScaleBasePlus;
    
    for (int i=0; i<self.titleArr.count; i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(60*kUIScaleBasePlus+width*i,0,width,height)
                                            textAligment:NSTextAlignmentCenter
                                                    text:self.titleArr[i]
                                                textClor:LCHexColor(0x7C8390)
                                               buttonBGC:[UIColor clearColor]
                                                textFont:16*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(btnClick:)];
        if (i == 0)
        {
            [btn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];

        }
        btn.enabled = NO;
        [self.buttonArr addObject:btn];
        [self addSubview:btn];
        
    }
}

#pragma mark - segment切换
- (void)changeColor:(NSInteger)num
{
    
    [self.buttonArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
         UIButton *btn = obj;
        
        if (idx != num)
        {
            [btn setTitleColor:LCHexColor(0x7C8390) forState:UIControlStateNormal];
            if (self.getBackBoyAndGirlBlock)
            {
                self.getBackBoyAndGirlBlock(num);
            }
        }
        else
        {
            [btn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
            if (self.getBackBoyAndGirlBlock)
            {
                self.getBackBoyAndGirlBlock(num-2);
            }
        }
        
    }];
}

#pragma mark - 按钮点击
- (void)btnClick:(UIButton *)sender
{
    
}
@end
